<?php

!defined('DEBUG') AND exit('Access Denied.');

$action = param(1);
if(!admin_authority_check()) header("Location:".url('index-login'));
if($action == 'gateway') {

	if($method=='GET'){
		$page = param('page', 1);
		$pagesize = 10;
		$gateway = db_find('gateway', array(), array(), $page, $pagesize);	
		$pagination = pagination(url("pay-gateway")."?page={page}", db_count('gateway'), $page, $pagesize);
		$header['title'] = '网关管理 - '.$conf['sitename'];
		include _include(ADMIN_PATH."view/htm/pay_gateway.htm");
	}else{
		$passcode = param('passcode');
		if(md5(md5($passcode).'ryTY45Rg')!=$conf['passcode']){
			message(0, '安全码错误');
		}
		$name = param('name', array());
		$id = param('id', array());
		$secret = param('secret', array());
		$publickey = param('publickey', array());
      	$url = param('url', array());
		foreach($name as $k=>$n){
			$i = $id[$k];
			$s = $secret[$k];
			$p = $publickey[$k];
          	$u = $url[$k];
			db_update('gateway', array('id'=>$k), array('name'=>$n,'api_id'=>$i,'api_secret'=>$s,'api_publickey'=>$p,'url'=>$u));
		}
		message(1, '修改成功');
	}
	
} elseif($action == 'gatewayadd') {

	if($method == 'GET') {
		$header['title'] = "添加通道";
		$header['mobile_title'] = "添加通道";
        $users = db_find('user',array('is_admin'=>0,'is_proxy'=>0));
        $cate = db_find('cate',array('is_public'=>1,'status'=>1),array(),1,1000);
        $area = db_find('area', array(), array(), 1, 1000);
		include _include(ADMIN_PATH."view/htm/pay_gatewayadd.htm");

	} elseif($method == 'POST') {
		$passcode = param('passcode');
		if(md5(md5($passcode).'ryTY45Rg')!=$conf['passcode']){
			message(0, '安全码错误');
		}
		$array = array();
		$array['shortname'] = param('shortname');
		$array['name'] = param('name');
		$array['api_id'] = param('api_id');
		$array['api_secret'] = param('api_secret');
		$array['api_publickey'] = param('api_publickey');
		$array['url'] = param('url');
		$r = db_insert('gateway', $array);
		$r === FALSE AND message(-1, "添加失败");
		message(0, "添加成功");
	}

}
else if($action == 'gatewayup'){
    $id = $_GET['2'];
//        if(empty($id)){
//            message(0, '没有选择数据');
//        }
    $header['title'] = "修改通道";
    $header['mobile_title'] = "修改通道";
//    $users = db_find('user', array('is_open'=>1,'is_admin'=>0,'is_proxy'=>0), array(), 1, 1000);
    $users = db_find('user',array('is_admin'=>0,'is_proxy'=>0));
//    $users = db_find('user', array('is_proxy'=>1), array(), 1, 1000);
//        var_dump($users);exit;
    $cate = db_find('cate',array('is_public'=>1,'status'=>1),array(),1,1000);
    $area = db_find('area', array(), array(), 1, 1000);

    $b = db_find_one('channel',array('id'=>$id));
    $a= db_find_one('gateway',array('id'=>$b['gate_id']));

    include _include(ADMIN_PATH."view/htm/pay_gatewayup.htm");

}else if($action == 'gatewaydel'){
	$passcode = param('passcode');
	if(md5(md5($passcode).'ryTY45Rg')!=$conf['passcode']){
		message(0, '安全码错误');
	}
		
	$id = param('id');
	//删除网关
	db_delete('gateway', array('id'=>$id));
	//删除通道
	db_delete('channel', array('gate_id'=>$id));
	
	message(1, '删除成功');

}else if($action == 'upstatus'){
    $id = param('id');
    $status = param('status');
    if(empty($id)){
        message(-1, '数据缺失');
    }
    $re = db_update('channel',array('id'=>$id),array('is_open'=>$status));

    $re === FALSE AND message(-1, "设置失败");
    message(1, "设置成功");
}else if($action == 'delchannerl'){
    db_sql_find_one('SET AUTOCOMMIT=0');
    $id = param('id');
    $arr = db_find_one('channel',array('id'=>$id));
    if(!empty($arr)){
        $gate_id = $arr['gate_id'];
        $re = db_delete('channel',array('id'=>$id));
        if($re==FALSE){
            db_sql_find_one('ROLLBACK');
            message(-1, "删除失败");
        }
        $r =  db_delete('gateway',array('id'=>$gate_id));
        if($r==FALSE){
            db_sql_find_one('ROLLBACK');
            message(-1, "删除失败");
        }
        db_sql_find_one('COMMIT');
    }
    message(1, "删除成功");

}
else if($action == 'channel') {

	if($method=='GET'){
		$gateway = db_find('gateway', array(), array(), 1, 1000);
		$area = db_find('area', array(), array(), 1, 1000);
		$cate = db_find('cate', array(), array(), 1, 1000);
		//参数
		$page = param('page', 1);
		$page = $page<1?1:$page;
		$pagesize = 10;
		$type = param('type', 0);	// 搜索类型
		//条件
		$cond='';
	    if($type!=0){
	        $cond = ' and a.uid='.$type;
        }
        $sql1 = "select count(*)as num from pay_channel a INNER join pay_gateway b on a.gate_id=b.id INNER JOIN pay_cate c on c.id=a.cate INNER JOIN pay_user d on d.uid=a.uid".$cond;
		$num = db_sql_find_one($sql1);
		$num = $num['num'];
		$allpage = ceil($num/$pagesize);
		$page = $page>$allpage?$allpage:$page;
		$start = ($page-1)*$pagesize;
		$sql = "select a.*,b.shortname,b.name as gname,b.api_id,b.api_secret,b.api_publickey,b.url,b.status,c.name as catename,d.username from pay_channel a INNER join pay_gateway b on a.gate_id=b.id INNER JOIN pay_cate c on c.id=a.cate INNER JOIN pay_user d on d.uid=a.uid".$cond;
		$sql.=$cond;
		$sql.=" limit $start,$pagesize";
		//查询
		$channel = db_sql_find($sql);
		$pagination = pagination(url("pay-channel")."?page={page}&type=$type",$num, $page, $pagesize);
		$usersarr = db_find('user',array('is_admin'=>0,'is_proxy'=>0));
		$header['title'] = '通道管理 - '.$conf['sitename'];
		include _include(ADMIN_PATH."view/htm/pay_channel.htm");
	}else{
		$passcode = param('passcode');
		if(md5(md5($passcode).'ryTY45Rg')!=$conf['passcode']){
			message(0, '安全码错误');
		}
		$name = param('name', array());
		$alipay_account = param('alipay_account', array());
		$alipay_realname = param('alipay_realname', array());
		$gid = param('gateway', array());		
      	$max = param('max', array());
		$open = param('is_open', array());
		$area = _POST('area');
		$host = param('host', array());
		$cate = param('cate', array());		
		foreach($name as $k=>$n){
			//地区
			$area_str='';
			if(!empty($area[$k])){
				$area_str=implode(',',$area[$k]);
			}
			$channel_arr=array('name'=>$n, 'alipay_account'=>$alipay_account[$k], 'alipay_realname'=>$alipay_realname[$k], 'gate_id'=>$gid[$k], 'max_limit'=>$max[$k], 'is_open'=>$open[$k], 'area'=>$area_str, 'host'=>$host[$k], 'cate'=>$cate[$k]);
			//状态
			$channel = db_find_one('channel', array('id' => $k));
			if($channel['is_open']==1 && $open[$k]==0 && $channel['close_reason']!=0){//手动关闭通道
				$channel_arr=array_merge($channel_arr, array('close_reason'=>0));
			}elseif($channel['is_open']==0 && $open[$k]==1){
				$channel_arr=array_merge($channel_arr, array('overtimes_starttime'=>$time));//更新订单失败次数的起始时间
			}
			db_update('channel', array('id'=>$k), $channel_arr);
		}
		message(1, '修改成功');
	}
	
}elseif($action == 'channeladd') {

	if($method == 'GET') {
		$header['title'] = "添加通道";
		$header['mobile_title'] = "添加通道";
		$gateway = db_find('gateway', array(), array(), 1, 1000);
		$area = db_find('area', array(), array(), 1, 1000);
		$cate = db_find('cate', array(), array(), 1, 1000);
		include _include(ADMIN_PATH."view/htm/pay_channeladd.htm");

	} elseif($method == 'POST') {
		$passcode = param('passcode');
		if(md5(md5($passcode).'ryTY45Rg')!=$conf['passcode']){
			message(0, '安全码错误');
		}
		$array = array();
		$array['name'] = param('name');
		$array['alipay_account'] = param('alipay_account');
		$array['alipay_realname'] = param('alipay_realname');
		$array['gate_id'] = param('gate_id');		
		$array['max_limit'] = param('max_limit');
		$array['host'] = param('host');
		$array['cate'] = param('cate');
		$array['is_open'] = param('is_open');
		//
		$area = _POST('area');
		$area_str='';
		if(!empty($area)){
			$area_str=implode(',',$area);
		}
		$array['area'] = $area_str;
		//
		$r = db_insert('channel', $array);
		$r === FALSE AND message(-1, "添加失败");
		message(0, "添加成功");
	}

}else if($action == 'channeldel'){
	$passcode = param('passcode');
	if(md5(md5($passcode).'ryTY45Rg')!=$conf['passcode']){
		message(0, '安全码错误');
	}
		
	$id = param('id');
	//删除通道
	db_delete('channel', array('id'=>$id));
	
	message(1, '删除成功');

}else if($action == 'bank') {

	if($method=='GET'){
		$page = param('page', 1);
		$pagesize = 200;
		$bank = db_find('bank', array(), array(), $page, $pagesize);	
		$pagination = pagination(url("pay-bank")."?page={page}", db_count('bank'), $page, $pagesize);
		$header['title'] = '银行管理 - '.$conf['sitename'];
		include _include(ADMIN_PATH."view/htm/pay_bank.htm");
	}else{
		$name = param('name', array());
		$shortname = param('shortname', array());
		$open = param('is_open', array());
		foreach($name as $k=>$n){
			db_update('bank', array('id'=>$k), array('name'=>$n,'shortname'=>$shortname[$k], 'is_open'=>$open[$k]));
		}
		message(1, '修改成功');
	}
	
}else if($action == 'cate') {
	if($method=='GET'){
		$page = param('page', 1);
		$pagesize = 200;
		$cate = db_find('cate', array(), array('id'=>1), $page, $pagesize);	
		$pagination = pagination(url("pay-cate")."?page={page}", db_count('cate'), $page, $pagesize);
		$header['title'] = '分类管理 - '.$conf['sitename'];
		include _include(ADMIN_PATH."view/htm/pay_cate.htm");
	}else{
		$name = param('name', array());
		foreach($name as $k=>$n){
			db_update('cate', array('id'=>$k), array('name'=>$n));
		}
		message(1, '修改成功');
	}
	
}elseif($action == 'cateadd') {

	if($method == 'POST') {
		$array = array();
		$array['name'] = param('name');
		$r = db_insert('cate', $array);
		$r === FALSE AND message(-1, "添加失败");
		message(1, "添加成功");
	}

}else if($action == 'catedel'){

	$id = param('id');
	//删除分类
	db_delete('cate', array('id'=>$id));
	//删除价格段
	db_delete('cate_price', array('cid'=>$id));
	//删除商品名称
	db_delete('cate_goods', array('cid'=>$id));
	
	message(1, '删除成功');

}else if($action == 'cateprice') {

	if($method=='GET'){
	
		$cid = param(2, 0);
		$cate = db_find_one('cate', array('id'=>$cid));
		if (empty($cate)) exit('分类错误');		
		$cate_name=$cate['name'];
		
		$page = param('page', 1);
		$pagesize = 200;
		$cond=array('cid'=>$cid);
		$cate_price = db_find('cate_price', $cond, array('price_start'=>1), $page, $pagesize);	
		$pagination = pagination(url("pay-cateprice-$cid")."?page={page}", db_count('cate_price', $cond), $page, $pagesize);
		$header['title'] = '价格段管理 - '.$conf['sitename'];
		include _include(ADMIN_PATH."view/htm/pay_cate_price.htm");
	}else{
		$price_start = param('price_start', array());
		$price_end = param('price_end', array());
		foreach($price_start as $k=>$n){
			db_update('cate_price', array('id'=>$k), array('price_start'=>$n, 'price_end'=>$price_end[$k]));
		}
		message(1, '修改成功');
	}
	
}elseif($action == 'catepriceadd') {

	if($method == 'POST') {
		$array = array();
		$array['cid'] = param('cid');
		$array['price_start'] = param('price_start');
		$array['price_end'] = param('price_end');
		$r = db_insert('cate_price', $array);
		$r === FALSE AND message(-1, "添加失败");
		message(1, "添加成功");
	}

}else if($action == 'catepricedel'){

	$id = param('id');
	//删除价格段
	db_delete('cate_price', array('id'=>$id));
	//删除商品名称
	db_delete('cate_goods', array('pid'=>$id));
	
	message(1, '删除成功');

}else if($action == 'categoods') {

	if($method=='GET'){
	
		$cid = param(2, 0);
		$cate = db_find_one('cate', array('id'=>$cid));
		if (empty($cate)) exit('分类错误');		
		$cate_name=$cate['name'];
		
		$pid = param(3, 0);
		$cate_price = db_find_one('cate_price', array('id'=>$pid));
		if (empty($cate_price)) exit('价格段错误');		
		$cate_price_name=$cate_price['price_start'].' - '.$cate_price['price_end'];
		
		$page = param('page', 1);
		$pagesize = 200;
		$cond=array('cid'=>$cid, 'pid'=>$pid);
		$cate_goods = db_find('cate_goods', $cond, array('id'=>1), $page, $pagesize);	
		$pagination = pagination(url("pay-categoods-$cid-$pid")."?page={page}", db_count('cate_goods', $cond), $page, $pagesize);
		$header['title'] = '商品名称管理 - '.$conf['sitename'];
		include _include(ADMIN_PATH."view/htm/pay_cate_goods.htm");
	}else{
		$name = param('name', array());
		foreach($name as $k=>$n){
			db_update('cate_goods', array('id'=>$k), array('name'=>$n));
		}
		message(1, '修改成功');
	}
	
}elseif($action == 'categoodsadd') {

	if($method == 'POST') {
		$array = array();
		$array['cid'] = param('cid');
		$array['pid'] = param('pid');
		$array['name'] = param('name');
		$r = db_insert('cate_goods', $array);
		$r === FALSE AND message(-1, "添加失败");
		message(1, "添加成功");
	}

}else if($action == 'categoodsdel'){

	$id = param('id');
	//删除商品名称
	db_delete('cate_goods', array('id'=>$id));
	
	message(1, '删除成功');

}else if($action == 'area') {

	if($method=='GET'){
		$page = param('page', 1);
		$pagesize = 200;
		$area = db_find('area', array(), array('id'=>1), $page, $pagesize);	
		$pagination = pagination(url("pay-area")."?page={page}", db_count('area'), $page, $pagesize);
		$header['title'] = '地区管理 - '.$conf['sitename'];
		include _include(ADMIN_PATH."view/htm/pay_area.htm");
	}else{
		$name = param('name', array());
		$area = param('area', array());
		$sort = param('sort', array());
		foreach($name as $k=>$n){
			db_update('area', array('id'=>$k), array('name'=>$n, 'area'=>$area[$k], 'sort'=>$sort[$k]));
		}
		message(1, '修改成功');
	}
	
}elseif($action == 'areaadd') {

	if($method == 'POST') {
		$array = array();
		$array['name'] = param('name');
		$array['area'] = param('area');
		$array['sort'] = param('sort');
		$r = db_insert('area', $array);
		$r === FALSE AND message(-1, "添加失败");
		message(1, "添加成功");
	}

}else if($action == 'areadel'){

	$id = param('id');
	//删除地区
	db_delete('area', array('id'=>$id));
	
	message(1, '删除成功');

}else if($action == 'successrate') {

	$channels = db_find('channel', array(), array(), 1, 1000);
	//搜索参数
	$channel = param('channel', 0);	// 通道
	$fdate = param('fdate', date('m/d/Y', strtotime('today')));	// 开始时间
	$tdate = param('tdate', date('m/d/Y', strtotime('today')));	// 结束时间
	//搜索条件
	$where='';
	if($channel!=0) $where='where c.id='.$channel;
	$where_o=' and o.time>='.strtotime($fdate).' and o.time<'.(strtotime($tdate)+86400);
	//记录
	$log = db_sql_find("SELECT c.name, (SELECT count(id) FROM ".$db->tablepre."order as o WHERE o.chid = c.id $where_o) as nums, (SELECT count(id) FROM ".$db->tablepre."order as o WHERE o.chid = c.id and o.status=1 $where_o) as nums_success FROM ".$db->tablepre."channel as c $where order by c.id asc");
	$header['title'] = '通道成功率 - '.$conf['sitename'];
	include _include(ADMIN_PATH."view/htm/pay_successrate.htm");

}else if($action == 'hebingadd'){
//     db_sql_find("SET AUTOCOMMIT=0");
    $passcode = param('b_passcode');
//    echo $passcode;exit;
    if(md5(md5($passcode).'ryTY45Rg')!=$conf['passcode']){
        message(-1, '安全码错误');
    }
    $id = param('a_id');
    if(empty($id)){
        $a = array();
        $a['shortname'] = param('a_shortname');
        $a['name'] = param('a_name');
        $a['api_id'] = trim(param('a_api_id'));
        $a['api_secret'] = trim(param('a_api_secret'));
        $a['api_publickey'] = trim(param('a_api_publickey'));
        $a['url'] = param('a_url');
        foreach ($a as $v){
            if(!$v || empty($v)){
                message(-1, "添加失败1");
            }
        }
        $re = db_insert('gateway', $a);
        if($re===FALSE){
            message(-1, "添加失败2");
        }
        $b = array();
        $b['name'] = param('a_name');
        $b['alipay_account'] = param('b_alipay_account');
        $b['alipay_realname'] = param('b_alipay_realname');
        $b['uid'] = param('b_user_id');
        $b['max_limit'] = param('b_max_limit');
        $b['gate_id'] = $re;
        $b['max_price'] = param('b_max_price');
        $b['cate'] = param('b_cate');
        $b['is_open'] = param('b_is_open');
        $b['host'] = '127.0.0.1';
        foreach ($b as $v){
            if(!$v || empty($v)){
                message(-1, "添加失败3");
            }
        }
        $b['area'] = param('b_area');
        $r = db_insert('channel', $b);
        if($r===FALSE){
            db_delete('gateway',array('id'=>$re));
            message(-1, "添加失败4");
        }
        message(0, "添加成功");
    }else{
        $a = array();
        db_sql_find_one('SET AUTOCOMMIT=0');
        $a['shortname'] = param('a_shortname');
        $a['name'] = param('a_name');
        $a['api_id'] = trim(param('a_api_id'));
        $a['api_secret'] = trim(param('a_api_secret'));
        $a['api_publickey'] = trim(param('a_api_publickey'));
        $a['url'] = param('a_url');
        foreach ($a as $v){
            if(!$v || empty($v)){
                message(-1, "修改失败");
            }
        }
        $re = db_update('gateway',array('id'=>$id),$a);
        if($re===FALSE){
            db_sql_find_one('ROLLBACK');
            message(-1, "修改失败");
        }
        $b = array();
        $b['name'] = param('a_name');
        $b['alipay_account'] = param('b_alipay_account');
        $b['alipay_realname'] = param('b_alipay_realname');
        $b['uid'] = param('b_user_id');
        $b['max_limit'] = param('b_max_limit');
        $b['cate'] = param('b_cate');
        $b['is_open'] = param('b_is_open');
        $b['max_price'] = param('b_max_price');
//        echo $id;exit;
        foreach ($b as $v){
            if(!$v || empty($v)){
                message(-1, "修改失败");
            }
        }
        $b['area'] = param('b_area');
        $r = db_update('channel',array('gate_id'=>$id),$b);
        if($r===FALSE){
            db_sql_find_one('ROLLBACK');
            message(-1, "修改失败");
        }
        db_sql_find_one('COMMIT');
        message(0, "修改成功");
    }


}else if($action=='hebingindex'){
    $arr = db_find("channel",array(),array(),1,1000);
    echo '<pre>';
    var_dump($arr);
}elseif($action=='shops'){
    $header['title'] = '分类管理 - '.$conf['sitename'];
    include _include(ADMIN_PATH."view/htm/pay_shops.htm");
}
?>